package com.lagou.bean;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.lagou.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

@Component
public class ComsumerComponet {

    @Reference
    private HelloService helloService;

    private int i = 0;

    private static int coreSize = Runtime.getRuntime().availableProcessors();

    private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("hello-pool-%d").build();

    private static ExecutorService pool = new ThreadPoolExecutor(coreSize, coreSize * 2
            , 0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>(10), THREAD_FACTORY, new ThreadPoolExecutor.DiscardOldestPolicy());


    public String sayHelloA(String name) {
        String helloA = helloService.sayHelloA(name);
        return helloA;
    }

    public String sayHelloB(String name) {
        String helloB = helloService.sayHelloB(name);
        return helloB;
    }

    public String sayHelloC(String name) {
        String helloC = helloService.sayHelloC(name);
        return helloC;
    }


    public void sayHello(String name, long startTime) {
        pool.submit(() -> {
            sayHelloA(name);
            sayHelloB(name);
            sayHelloC(name);
        });
        i++;
        if (i == 2000) {
            long l = System.currentTimeMillis();
            long time = l - startTime;
            System.out.println("2000次时间为：" + time + "ms");
        }
    }


}
